package com.mobilebsc.httpclient.callback;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONException;

import android.util.Log;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.mobilebsc.datatype.SignalAssociation;
import com.mobilebsc.exception.NotImplementedException;

public class SignalAssociationRestClientUsage implements
		IRestClientUsage<SignalAssociation>,
		IAsyncTaskCompleteListener<List<SignalAssociation>> {

	private static final String TAG = "SignalAssociationRestClientUsage";
	private IAsyncTaskCompleteListener<List<SignalAssociation>> callback;
	private String svcUrl;

	public SignalAssociationRestClientUsage(
			IAsyncTaskCompleteListener<List<SignalAssociation>> callback) {
		this.callback = callback;
		this.svcUrl = "signalassociation";
	}

	@Override
	public void onTaskComplete(List<SignalAssociation> result) {
		Log.i(TAG, "result[0].content: "
				+ result.get(0).getWeakSignal().getName() + ", "
				+ result.get(0).getKpi().getName() + ", "
				+ result.get(0).getInfluence());

		callback.onTaskComplete(result);
	}

	@Override
	public void getAll() {
		throw new NotImplementedException();

	}

	@Override
	public void getById(int id) {
		throw new NotImplementedException();

	}

	@Override
	public void add(SignalAssociation element) {
		try {
			StringEntity entity = new StringEntity(element.getJSON().toString());
			MbscRestClient.post(svcUrl + "/write", null, entity,
					new AsyncHttpResponseHandler());
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, "add = " + e.toString());
		} catch (JSONException e) {
			Log.e(TAG, "add = " + e.toString());
		}
	}

	@Override
	public void delete(SignalAssociation element) {
		try {
			StringEntity entity = new StringEntity(element.getJSON().toString());
			MbscRestClient.post(svcUrl + "/delete", null, entity,
					new AsyncHttpResponseHandler());
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, "delete = " + e.toString());
		} catch (JSONException e) {
			Log.e(TAG, "delete = " + e.toString());
		}
	}
}